package com.example.mybatisdemo.mapper;


import com.example.mybatisdemo.model.UserInfo;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserInfoMapper {
    // 查询所有用户
    @Select("select id,username,password,age,phone,delete_flag as deleteFlag," +
            "create_time as createTime, update_time as updateTime from user_info")
    public List<UserInfo> queryAllUser();

    // 通过 id 查询用户
    @Select("select * from user_info where id = #{id}")
    public UserInfo queryUserById(Integer id);

    // 增加数据

    @Options(useGeneratedKeys = true, keyProperty = "id")
    @Insert("insert into user_info (username, password, age, gender) values " +
            "(#{username},#{password},#{age},#{gender})")
    Integer insert(UserInfo userInfo);

    // 删除指定 id 的用户
    @Delete("delete from user_info where id = #{id}")
    Integer deleteById(Integer id);

    // 修改指定用户的 username
    @Update("update user_info set username = #{username} where id = #{id}")
    Integer updateNameById(UserInfo userInfo);

    // 测试结果映射

    @Select("select * from user_info")
    @Results({
            @Result(column = "delete_flag",property = "deleteFlag"),
            @Result(column = "create_time",property = "createTime"),
            @Result(column = "update_time",property = "updateTime")
    })
    List<UserInfo> queryAllUserInResult();

    // 测试配置转换
    @Select("select * from user_info")
    List<UserInfo> queryAllUserInConfig();

    // 测试 ${} 排序

    @Select("select * from user_info order by id ${sort}")
    List<UserInfo> queryAllUserBySort(String sort);

    // like 查询
//    @Select("select * from user_info where username like '%${key}%'")
    @Select("select * from user_info where username like concat('%',#{key},'%')")
    List<UserInfo> queryAllUserByLike(String key);


}
